package plugin;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import play.mvc.Router;
import play.templates.JavaExtensions;
import org.apache.commons.lang3.StringUtils;
import utils.*;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 模版对象拓展工具
 * @author zhangzc@djcars.cn
 */
public class XJavaExtensions extends JavaExtensions {

    /** 数字工具:S **/

    public static String prettyPrice(Double num){
        if(num == null){
            return String.valueOf(0);
        }
        if(num % 1 == 0){// 是这个整数，小数点后面是0
            return String.valueOf(Math.round(num));
        }else{//不是整数，小数点后面不是0
            DecimalFormat df = new DecimalFormat("######0.00");
            return df.format(num);
        }
    }

    /** 数字工具:E **/



    /** 时间工具:S **/

    public static String simpleDate(Date date) {
        if(date == null){
            return "未知";
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        int y1=c1.get(Calendar.YEAR);

        Date nowDate = new Date();
        Calendar c2 = Calendar.getInstance();
        c2.setTime(nowDate);
        int y2=c2.get(Calendar.YEAR);

        if(y1 == y2){
            DateFormat dateFormat = new SimpleDateFormat("M月d日");
            return dateFormat.format(date);
        }else{
            DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            return dateFormat.format(date);
        }
    }

    /** 时间工具:E **/


    /** Gson 对象工具:S **/

    public static String toJson(Object obj){
        if(obj == null){
            return "{}";
        }
        return JSON.toJSONString(obj);
    }

    /**
     * 获取Int
     * @param e
     * @param memName
     * @return
     */
    public static Integer getInt(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsInt();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsInt();
    }

    /**
     * 获取Long
     * @param e
     * @param memName
     * @return
     */
    public static Long getLong(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsLong();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsLong();
    }


    /**
     * 获取String
     * @param e
     * @param memName
     * @return
     */
    public static String getString(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsString();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsString();
    }

    /**
     * 获取JsonObject
     * @param e
     * @param memName
     * @return
     */
    public static JsonObject getJsonObject(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsJsonObject();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsJsonObject();
    }

    /**
     * 获取Boolean
     * @param e
     * @param memName
     * @return
     */
    public static Boolean getBoolean(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsBoolean();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsBoolean();
    }

    /**
     * 获取Double
     * @param e
     * @param memName
     * @return
     */
    public static Double getDouble(JsonElement e, String memName){
        if(e == null){
            return null;
        }
        if(StringUtils.isEmpty(memName)){
            return e.getAsDouble();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        return memE.getAsDouble();
    }

    /**
     * 获取列表
     * @param e
     * @param memName
     * @return
     */
    public static List<JsonElement> getList(JsonElement e, String memName){
        if(e == null){
            return Collections.emptyList();
        }
        JsonElement memE = e.getAsJsonObject().get(memName);
        if(memE == null){
            return null;
        }
        JsonArray arr = memE.getAsJsonArray();
        List<JsonElement> objs = new ArrayList(arr.size());
        for (int i = 0; i < arr.size(); i++) {
            objs.add(arr.get(i));
        }
        return objs;
    }

    /**
     * 作为列表
     * @param e
     * @return
     */
    public static List<JsonElement> asList(JsonElement e){
        if(e == null){
            return Collections.emptyList();
        }
        JsonArray arr = e.getAsJsonArray();
        List<JsonElement> objs = new ArrayList(arr.size());
        for (int i = 0; i < arr.size(); i++) {
            objs.add(arr.get(i));
        }
        return objs;
    }
    /** Gson 对象工具:E **/
}
